#!/bin/sh /etc/rc.common
# Copyright (C) 2018 Anton Kikin <a.kikin@tano-systems.com>
# Copyright (C) 2008-2015 OpenWrt.org

START=90
STOP=01

USE_PROCD=1
LLDPDBIN=/usr/sbin/lldpd
LLDPCLI=/usr/sbin/lldpcli
LLDPSOCKET=/var/run/lldpd.socket
LLDPD_CONF=/tmp/lldpd.conf
LLDPD_CONFS_DIR=/tmp/lldpd.d

LLDPD_RUN=/var/run/lldpd
LLDPD_RESTART_HASH=${LLDPD_RUN}/lldpd.restart_hash

. "$IPKG_INSTROOT/lib/functions/network.sh"
. "$IPKG_INSTROOT/usr/share/libubox/jshn.sh"

find_release_info()
{
	[ -s /etc/os-release ] && . /etc/os-release
	[ -z "$PRETTY_NAME" ] && [ -s /etc/openwrt_version ] && \
		PRETTY_NAME="$(cat /etc/openwrt_version)"

	echo "${PRETTY_NAME:-Unknown OpenWrt release} @ $(cat /proc/sys/kernel/hostname)"
}

get_config_restart_hash() {
	local var="$1"
	local _string _hash v

	config_load 'lldpd'

	config_get      v 'config' 'lldp_class'; append _string "$v" ","
	config_get      v 'config' 'agentxsocket'; append _string "$v" ","
	config_get      v 'config' 'cid_interface'; append _string "$v" ","
	config_get      v 'config' 'filter'; append _string "$v" ","
	config_get_bool v 'config' 'readonly_mode'; append _string "$v" ","
	config_get_bool v 'config' 'lldpmed_no_inventory'; append _string "$v" ","

	config_get_bool v 'config' 'enable_lldp' 1; append _string "$v" ","
	config_get_bool v 'config' 'force_lldp'; append _string "$v" ","

	config_get_bool v 'config' 'enable_cdp'; append _string "$v" ","
	config_get      v 'config' 'cdp_version'; append _string "$v" ","
	config_get_bool v 'config' 'force_cdp'; append _string "$v" ","
	config_get_bool v 'config' 'force_cdpv2'; append _string "$v" ","

	config_get_bool v 'config' 'enable_edp'; append _string "$v" ","
	config_get_bool v 'config' 'force_edp'; append _string "$v" ","

	config_get_bool v 'config' 'enable_fdp'; append _string "$v" ","
	config_get_bool v 'config' 'force_fdp'; append _string "$v" ","

	config_get_bool v 'config' 'enable_sonmp'; append _string "$v" ","
	config_get_bool v 'config' 'force_sonmp'; append _string "$v" ","

	_hash=`echo -n "${_string}" | md5sum | awk '{ print \$1 }'`
	export -n "$var=$_hash"
}

get_config_cid_ifaces() {
	local _ifaces
	config_get _ifaces 'config' 'cid_interface'

	local _iface _ifnames=""
	for _iface in $_ifaces; do
		local _ifname=""
		if network_get_device _ifname "$_iface" || [ -e "/sys/class/net/$_iface" ]; then
			append _ifnames "${_ifname:-$_iface}" ","
		fi
	done

	export -n "${1}=$_ifnames"
}

write_lldpd_conf()
{
	local lldp_description
	local lldp_hostname
	local lldp_platform
	local lldp_tx_interval
	local lldp_tx_hold
	local lldp_sys_mgmt_ip
	local lldp_agetntype
	local lldp_portidsubtype

	config_load 'lldpd'
	config_get lldp_description 'config' 'lldp_description' "$(find_release_info)"
	config_get lldp_hostname 'config' 'lldp_hostname' "$(cat /proc/sys/kernel/hostname)"
	config_get lldp_platform 'config' 'lldp_platform' ""
	config_get lldp_tx_interval 'config' 'lldp_tx_interval' 0
	config_get lldp_tx_hold 'config' 'lldp_tx_hold' 0
	config_get lldp_sys_mgmt_ip 'config' 'lldp_sys_mgmt_ip' ''
	config_get lldp_agenttype 'config' 'lldp_agenttype' 'nearest-bridge'
	config_get lldp_portidsubtype 'config' 'lldp_portidsubtype' 'macaddress'

	local ifaces
	config_get ifaces 'config' 'interface'

	local iface ifnames=""
	for iface in $ifaces; do
		local ifname=""
		if network_get_device ifname "$iface" || [ -e "/sys/class/net/$iface" ]; then
			append ifnames "${ifname:-$iface}" ","
		fi
	done

	# Clear out the config file first
	echo -n > "$LLDPD_CONF"
	[ -n "$ifnames" ] && echo "configure system interface pattern" "$ifnames" >> "$LLDPD_CONF"
	[ $lldp_tx_interval -gt 0 ] && echo "configure lldp tx-interval" "$lldp_tx_interval" >> "$LLDPD_CONF"
	[ $lldp_tx_hold -gt 0 ] && echo "configure lldp tx-hold" "$lldp_tx_hold" >> "$LLDPD_CONF"
	[ -n "$lldp_description" ] && echo "configure system description" "\"$lldp_description\"" >> "$LLDPD_CONF"
	[ -n "$lldp_hostname" ] && echo "configure system hostname" "\"$lldp_hostname\"" >> "$LLDPD_CONF"
	[ -n "$lldp_platform" ] && echo "configure system platform" "\"$lldp_platform\"" >> "$LLDPD_CONF"
	[ -n "$lldp_sys_mgmt_ip" ] && echo "configure system ip management pattern" "\"$lldp_sys_mgmt_ip\"" >> "$LLDPD_CONF"
	[ -n "$lldp_agenttype" ] && echo "configure lldp agent-type" "\"$lldp_agenttype\"" >> "$LLDPD_CONF"
	[ -n "$lldp_portidsubtype" ] && echo "configure lldp portidsubtype" "\"$lldp_portidsubtype\"" >> "$LLDPD_CONF"

	# Since lldpd's sysconfdir is /tmp, we'll symlink /etc/lldpd.d to /tmp/$LLDPD_CONFS_DIR
	[ -e $LLDPD_CONFS_DIR ] || ln -s /etc/lldpd.d $LLDPD_CONFS_DIR
}


start_service() {
	local enable_cdp
	local enable_fdp
	local enable_sonmp
	local enable_edp
	local lldp_class
	local lldp_location
	local lldpmed_no_inventory
	local readonly_mode
	local agentxsocket
	local filter

	local enable_lldp
	local force_lldp
	local cdp_version
	local force_cdp
	local force_cdpv2
	local force_edp
	local force_fdp
	local force_sonmp

	config_load 'lldpd'
	config_get_bool enable_cdp 'config' 'enable_cdp' 0
	config_get_bool enable_fdp 'config' 'enable_fdp' 0
	config_get_bool enable_sonmp 'config' 'enable_sonmp' 0
	config_get_bool enable_edp 'config' 'enable_edp' 0
	config_get lldp_class 'config' 'lldp_class'
	config_get lldp_location 'config' 'lldp_location'
	config_get_bool lldpmed_no_inventory 'config' 'lldpmed_no_inventory' 0
	config_get_bool readonly_mode 'config' 'readonly_mode' 0
	config_get agentxsocket 'config' 'agentxsocket'
	config_get filter 'config' 'filter' 15

	config_get_bool enable_lldp 'config' 'enable_lldp' 1
	config_get_bool force_lldp 'config' 'force_lldp' 0
	config_get cdp_version 'config' 'cdp_version' 'cdpv1v2'
	config_get_bool force_cdp 'config' 'force_cdp' 0
	config_get_bool force_cdpv2 'config' 'force_cdpv2' 0
	config_get_bool force_edp 'config' 'force_edp' 0
	config_get_bool force_fdp 'config' 'force_fdp' 0
	config_get_bool force_sonmp 'config' 'force_sonmp' 0

	local readonly_mode
	config_get_bool readonly_mode 'config' 'readonly_mode' 0

	mkdir -p ${LLDPD_RUN}
	chown lldpd:lldpd ${LLDPD_RUN}

	# When lldpd starts, it also loads up what we write in this config file
	write_lldpd_conf

	procd_open_instance
	procd_set_param command ${LLDPDBIN}
	procd_append_param command -d # don't daemonize, procd will handle that for us

	if [ $enable_lldp -gt 0 ]; then
		if [ $force_lldp -gt 0 ]; then
			procd_append_param command '-l'
		fi
	else
		# Disable LLDP
		procd_append_param command '-ll'
	fi

	if [ $enable_cdp -gt 0 ]; then
		if [ $cdp_version == "cdpv1v2" ]; then
			if [ $force_cdp -gt 0 ]; then
				if [ $force_cdpv2 -gt 0 ]; then
					# CDPv1 enabled, CDPv2 forced
					procd_append_param command '-ccc'
				else
					# CDPv1 forced, CDPv2 enabled
					procd_append_param command '-cc'
				fi
			else
				# CDPv1 and CDPv2 enabled
				procd_append_param command '-c'
			fi
		elif [ $cdp_version == "cdpv2" ]; then
			if [ $force_cdp -gt 0 ]; then
				# CDPv1 disabled, CDPv2 forced
				procd_append_param command '-ccccc'
			else
				# CDPv1 disabled, CDPv2 enabled
				procd_append_param command '-cccc'
			fi
		fi
	fi
	
	if [ $enable_fdp -gt 0 ]; then
		if [ $force_fdp -gt 0 ]; then
			procd_append_param command '-ff'
		else
			procd_append_param command '-f'
		fi
	fi

	if [ $enable_edp -gt 0 ]; then
		if [ $force_edp -gt 0 ]; then
			procd_append_param command '-ee'
		else
			procd_append_param command '-e'
		fi
	fi

	if [ $enable_sonmp -gt 0 ]; then
		if [ $force_sonmp -gt 0 ]; then
			procd_append_param command '-ss'
		else
			procd_append_param command '-s'
		fi
	fi

	[ $readonly_mode -gt 0 ] && procd_append_param command '-r'
	[ $lldpmed_no_inventory -gt 0 ] && procd_append_param command '-i'
	[ -n "$lldp_class" ] && procd_append_param command -M "$lldp_class"
	[ -n "$agentxsocket" ] && procd_append_param command -x -X "$agentxsocket"
	[ -n "$filter" ] && procd_append_param command -H "$filter"

	# ChassisID interfaces
	local ifnames
	get_config_cid_ifaces ifnames
	[ -n "$ifnames" ] && procd_append_param command -C "$ifnames"

	# Overwrite default configuration locations processed by lldpcli at start
	procd_append_param command -O "$LLDPD_CONF"

	local restart_hash
	get_config_restart_hash restart_hash
	echo -n "$restart_hash" > $LLDPD_RESTART_HASH

	# set auto respawn behavior
	procd_set_param respawn
	procd_close_instance
}

service_triggers() {
	procd_add_config_trigger "config.change" "lldpd" /etc/init.d/lldpd reload
}

lldpcli_running_config_read() {
	json_init
	json_load "`$LLDPCLI -f json show configuration`"
}

lldpcli_running_config_get_value() {
	local param=$1
	local value=""
	json_select "configuration"
		json_select "config"
			json_get_var value "$param"
		json_select ..
	json_select ..
	echo $value
}

reload_service() {
	running || return 1

	local running_hash=""
	local config_hash=""

	get_config_restart_hash config_hash
	if [ -f ${LLDPD_RESTART_HASH} ]; then running_hash=`cat $LLDPD_RESTART_HASH`; fi

	if [ "x$running_hash" != "x$config_hash" ]; then
		# Restart LLDPd
		# Some parameters can't be configured at runtime
		restart
		return 0
	fi

	$LLDPCLI -u $LLDPSOCKET &> /dev/null <<-EOF
		pause
		unconfigure lldp custom-tlv
		unconfigure system interface pattern
		unconfigure system description
		unconfigure system hostname
		unconfigure system platform
		unconfigure system ip management pattern
	EOF
	# Rewrite lldpd.conf
	# If something changed it should be included by the lldpcli call
	write_lldpd_conf
	$LLDPCLI -u $LLDPSOCKET -c $LLDPD_CONF -c $LLDPD_CONFS_DIR &> /dev/null
	# Broadcast update over the wire
	$LLDPCLI -u $LLDPSOCKET &> /dev/null <<-EOF
		resume
		update
	EOF
	return 0
}

stop_service() {
	rm -rf ${LLDPD_RUN} 2>/dev/null
}
